package com.niit.tsr.controller;

import com.niit.tsr.bean.Book;
import com.niit.tsr.bean.BorrowRecord;
import com.niit.tsr.repository.BorrowRepository;
import com.niit.tsr.repository.DetailRepository;
import com.niit.tsr.service.BorrowService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.transaction.Transactional;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Controller
public class DetailController {
    @Autowired
    private DetailRepository detailRepository;
    @Autowired
    private BorrowRepository borrowRepository;
    @Autowired
    private BorrowService borrowService;

    @GetMapping("/findBookid")
    public String findBookBid(@RequestParam(name = "book_id") String book_id, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("user_id", 123);
        Book book = detailRepository.findBookBid(book_id);
        model.addAttribute("book", book);
        return "886";
    }

    @GetMapping("/goTo")
    public String goToAddUser() {
        return "Detail";
    }


    @PostMapping ("/updateBook")
    @Transactional
     public String updateBook(@RequestParam(name = "book_id") String book_id, Model model,HttpServletRequest request ) {
        detailRepository.updateBook(book_id);

        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        BorrowRecord  borrowRecord = new BorrowRecord();
        borrowRecord.setBorrow_date(LocalDateTime.now());
        borrowRecord.setUser_id(user_id);
        int intbook_id = Integer.parseInt(book_id);
        borrowRecord.setBook_id(intbook_id);
        Book book = detailRepository.findBookBid(book_id);

        String book_name= book.getBook_name();
        borrowRecord.setBook_name(book_name);
        borrowService.borrow(borrowRecord);

        model.addAttribute("book", book);
        return "886";
       }

    @PostMapping ("/BorrowRecord")
    @Transactional
    public String BorrowRecord(@ModelAttribute("BorrowRecord") BorrowRecord borrowRecord, Model model) {
        borrowRepository.save(borrowRecord);
        model.addAttribute("book");
        return "886";
    }

    @GetMapping("/borrowPage")
    public String findUserId( Model model, HttpServletRequest request){

        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow";
    }
    @GetMapping("/borrow")
    public String findUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user_id", 123);
        return "redirect:/borrowPage";
    }

    @PostMapping("/deleteBorrowRecord")
    public String deleteBorrowRecord(@RequestParam("borrow_id") int borrow_id,Model model,HttpServletRequest request) {
        borrowRepository.deleteBorrowRecord(borrow_id);
        HttpSession session = request.getSession();

        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow";
    }

@PostMapping ("/replyBorrowRecord")
@Transactional
    public String updateBorrowDateByBookId(@RequestParam(name = "borrow_id") Integer borrow_id, Model model,HttpServletRequest request ){
        borrowRepository.updateBorrowDateByBookId(borrow_id);
        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow" ;
    }




}



